Danny Ward's late goal ended relegated Rotherham's 10-game losing streak and left Birmingham just four points above the Championship relegation zone.